<template>
  <px-dialog v-model="visible" width="640px" @close="closeDrawer" @open="openDrawer">
    <template #header>
      <h3 style="font-weight: bold">关联类型</h3>
    </template>
    <template #default>
      <px-form ref="formRef" :model="formValue" :rules="rules" label-position="right" label-width="80px">
        <px-form-item label="种类" prop="category">
          <px-select v-model="formValue.category" @change="handleCategoryChange" filterable clearable placeholder="请选择种类">
            <px-option v-for="item in categoryList" :key="item.value" :label="item.name" :value="item.value" />
          </px-select>
        </px-form-item>
        <px-form-item label="类型名称" prop="typeCode">
          <px-tree-select
            v-model="formValue.typeCode"
            placeholder="请选择类型名称"
            filterable
            clearable
            :data="typeList"
            :render-after-expand="false"
            :props="defaultProps"
            @visible-change="initTypeList"
          />
        </px-form-item>
      </px-form>
    </template>
    <template #footer>
      <px-button @click="closeDrawer">取消</px-button>
      <px-button type="primary" @click="handleSubmit(formRef)">保存</px-button>
    </template>
  </px-dialog>
</template>
<script setup lang="ts">
import { queryDictData } from "@/api/common";
import { saveSparePartBrandType, queryListTree_1 } from "@/api/equip/spareParts";
import { PxMessage } from "px-ui";
import { computed, ref, reactive, toRefs } from "vue";

// 调用父级-关闭抽屉、查询列表方法
const emits = defineEmits(["closeDrawer", "queryTable"]);

const formRef = ref();
const props = defineProps(["visible", "drawerType", "detailData"]);
const visible = computed(() => props.visible);

// 树形选择图
const defaultProps = {
  children: "children",
  label: "name",
  value: "code"
};
const state = reactive<any>({
  formValue: {},
  categoryList: [],
  typeList: []
});
const { formValue, categoryList, typeList } = toRefs(state);

// 表单校验规则
const rules = computed(() => ({
  typeCode: [
    {
      required: true,
      message: "类型名称不能为空",
      trigger: ["blur", "change"]
    }
  ]
}));
const openDrawer = async () => {
  initTypeList(true);
  initCategory();
  if (props.drawerType === "add") {
    formValue.value = {};
  } else {
    formValue.value = { ...props.detailData };
  }
};
// 种类改变，清空类型
const handleCategoryChange = () => {
  typeList.value = [];
  formValue.value.typeCode = undefined;
};
// 种类下拉框
const initCategory = async () => {
  categoryList.value = [];
  const params = {
    typeCode: "typeCategory"
  };
  const data = await queryDictData(params);
  categoryList.value = data;
};
// 类型下拉框
const initTypeList = async flag => {
  if (flag) {
    const data = await queryListTree_1({ category: formValue.value.category });
    typeList.value = data;
  } else {
    typeList.value = [];
  }
};
// 关闭抽屉
const closeDrawer = () => {
  formRef.value.resetFields();
  emits("closeDrawer");
};
// 提交
const handleSubmit = async (formEl: any) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      const params = JSON.parse(JSON.stringify(formValue.value));
      await saveSparePartBrandType({ typeCode: params.typeCode, brandCode: props.detailData?.code });
      PxMessage.success("关联成功");
      emits("closeDrawer");
      emits("queryTable");
    }
  });
};
</script>
<style lang="scss" scoped>
:deep(.px-select) {
  width: 100%;
}
</style>
